const db = require('../db/util')
const path = require('path')

let addArticle = (req,res)=>{
  console.log(req.file);
  // 将文件类型的数据，解析并挂载到 req.file 属性中
  // 将文本类型的数据，解析并挂载到 req.body 属性中
  if(!req.file || req.file.fieldname != "cover_img"){
    return res.cc("文章封面是必选参数！")
  }


  // console.log(req.body);
  const articleInfo = {
    //标题 内容 状态 所属分类id
    ...req.body,
    cover_img:path.join("/upload",req.file.filename),
    pub_date:new Date(),
    author_id:req.user.id,
  }

  const sql = 'insert into ev_articles set ?'
  db.query(sql,articleInfo,(err, result) => {

    if(err) return res.cc(err.message)

    if(result.affectedRows == 0 ) return res.cc("文章上传失败");

    res.cc("文章上传成功",0)

  })
}

module.exports = {

  addArticle,

}